﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XmlCreate
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }
        IODEF iODEF = new IODEF();
        private void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                iODEF.maxTagId = maxTagIdText.Text;
                iODEF.mode = modetext.Text;
                iODEF.version = versiontext.Text;


                string xml = EventXml.Serializer(typeof(IODEF), iODEF).Replace("<?xml version=\"1.0\"?>", "<?xml version=\"1.0\" encoding=\"gb2312\" standalone=\"yes\"?>").Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ", "");
                StreamWriter sw = new StreamWriter(string.IsNullOrEmpty(fileName.Text) ? "default.xml" : fileName.Text);//这里写上你要保存的路径
                sw.WriteLine(xml);//按行写
                sw.Close();//关闭
                MessageBox.Show("生成成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("生成失败!错误信息:"+ex.Message);
                throw;
            }
           
        }


        string desc;
        string name;
        private void Button2_Click(object sender, EventArgs e)
        {
            IOGROUPForm iOGROUPForm;
            if (iODEF.IOGROUP != null)
            {
                iOGROUPForm = new IOGROUPForm(desc, name, iODEF.IOGROUP);
            }
            else
	        {
                iOGROUPForm = new IOGROUPForm(desc, name, null);
	        }

            
            if (iOGROUPForm.ShowDialog() ==DialogResult.OK )
            {
                iODEF.IOGROUP = iOGROUPForm.IOGROUP;
            }
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            LOGROUPForm lOGROUPForm;;
            if (iODEF.LOGROUP != null)
            {
                lOGROUPForm = new LOGROUPForm(desc, name, iODEF.LOGROUP);

            }
            else
            {
                lOGROUPForm = new LOGROUPForm(desc, name, null);
            }
           
            if (lOGROUPForm.ShowDialog() == DialogResult.OK)
            {
                iODEF.LOGROUP = lOGROUPForm.iODEFLOGROUP;
            }
        }

        private void Openxmlbtn_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = false;//该值确定是否可以选择多个文件
                dialog.Title = "请选择文件";
                dialog.Filter = "所有文件(*.*)|*.*";
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string file = dialog.FileName;
                    iODEF = EventXml.Deserialize(typeof(IODEF), EventXml.GetStringByXml(file)) as IODEF;
                    maxTagIdText.Text = iODEF.maxTagId;
                    versiontext.Text = iODEF.version;
                    modetext.Text = iODEF.mode;
                    fileName.Text = file;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("打开的xml格式不正确");
            }
            
        }
    }
}
